package com.daocao.hua.activity;

import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import com.daocao.hua.R;
import com.daocao.hua.api.callback.PointRuleContract;
import com.daocao.hua.api.requestInfo.PointRuleRequest;
import com.daocao.hua.constant.Const;
import com.daocao.hua.model.PointRuleModel;
import com.daocao.hua.view.CircularProgress;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 2017/9/27.
 */

public class PointRuleActivity extends BaseActivity implements PointRuleContract.View {
    @BindView(R.id.txt_content)
    TextView txtContent;
    @BindView(R.id.progressBar)
    CircularProgress loading;
    private Unbinder unbinder;
    private PointRuleRequest api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.activity_point_rule);
        unbinder = ButterKnife.bind(this);
        baseInitView(this, "PointRuleActivity", Const.ViewType.ACTIVITY);
        initBar();
        init();
    }

    private void init() {
        setBackIntent();
        setTitleText("积分规则");
        api = PointRuleRequest.create();
        api.attachView(this);
        api.getRuleText();

    }

    private void showLoading(boolean b) {
        if (loading != null) {
            if (b)
                loading.setVisibility(View.VISIBLE);
            else
                loading.setVisibility(View.GONE);
        }
    }

    public void setSpanTitle(String title, StringBuilder sb) {
        sb.append("<br/><font color=\"#111111\"><big>");
        sb.append(title);
        sb.append("</big></font><br/>");
    }

    public void setSpanContent(String content, StringBuilder sb) {
        sb.append("<font color=\"#888888\">");
        sb.append(content);
        sb.append("</font><br/>");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        api.detachView();
        api = null;
        unbinder.unbind();
        unbinder = null;
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {
        showLoading(false);
    }

    @Override
    public void onRequestStart() {
        showLoading(true);
    }

    @Override
    public void getRuleTextSuccess(PointRuleModel item) {
        StringBuilder sb = new StringBuilder();
        List<PointRuleModel.DataBean> rules = item.data;
        for (int i = 0; i < rules.size(); i++) {
            setSpanTitle(rules.get(i).title, sb);
            for (int j = 0; j < rules.get(i).content.size(); j++) {
                setSpanContent(rules.get(i).content.get(j), sb);
            }
        }
        txtContent.setText(Html.fromHtml(sb.toString()));
    }


}
